﻿#region Copyright (c) 2013-03, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using Amarok.Agents.Resources;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_DefaultSubsystemFactory
	{
		private class MockSubsystem : Subsystem<StubEnvironment>
		{
			public Int32 LoadCalled;

			public new StubEnvironment Environment
			{
				get
				{
					return base.Environment;
				}
			}

			public MockSubsystem(StubEnvironment environment)
				: base(environment)
			{
			}

			protected override void OnLoad()
			{
				base.OnLoad();

				LoadCalled++;
			}
		}

		private class SubsystemWithInvalidConstructor : Subsystem
		{
			public SubsystemWithInvalidConstructor()
				: base(null)
			{
			}
		}

		private class SubsystemWithExceptionInConstructor : Subsystem
		{
			public SubsystemWithExceptionInConstructor(RuntimeEnvironment environment)
				: base(environment)
			{
				throw new ApplicationException("CTR");
			}
		}

		private class SubsystemWithExceptionInLoad : Subsystem
		{
			public SubsystemWithExceptionInLoad(RuntimeEnvironment environment)
				: base(environment)
			{
			}

			protected override void OnLoad()
			{
				throw new ApplicationException("LOAD");
			}
		}

		private class StubEnvironment : RuntimeEnvironment
		{
			public StubEnvironment(IRuntime runtime, IMessageBus messageBus)
				: base(runtime, messageBus)
			{
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_LoadSubsystem()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new StubEnvironment(runtime, messageBus);
			var factory = new DefaultSubsystemFactory();

			var subsystem = (MockSubsystem)factory.LoadSubsystem(
				typeof(MockSubsystem),
				environment);

			Assert.That(subsystem, Is.Not.Null);
			Assert.That(subsystem.Environment, Is.SameAs(environment));
			Assert.That(subsystem.LoadCalled, Is.EqualTo(1));
		}

		[Test]
		public void Test_LoadSubsystem_NullType()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new StubEnvironment(runtime, messageBus);
			var factory = new DefaultSubsystemFactory();

			Assert.Throws<ArgumentNullException>(() =>
				{
					factory.LoadSubsystem(
						null,
						environment);
				});
		}

		[Test]
		public void Test_LoadSubsystem_NonSubsystemType()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new StubEnvironment(runtime, messageBus);
			var factory = new DefaultSubsystemFactory();

			Assert.Throws<ArgumentException>(() =>
				{
					factory.LoadSubsystem(
						typeof(Version),
						environment);
				});
		}

		[Test]
		public void Test_LoadSubsystem_TypeWithInvalidConstructor()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new StubEnvironment(runtime, messageBus);
			var factory = new DefaultSubsystemFactory();

			var exception = Assert.Throws<SubsystemLoadException>(() =>
				{
					factory.LoadSubsystem(
						typeof(SubsystemWithInvalidConstructor),
						environment);
				});

			Assert.That(exception.Message, Is.EqualTo(SubsystemResources.SubsystemTypeDoesNotProvideSubsystemConstructor));
		}

		[Test]
		public void Test_LoadSubsystem_TypeWithExceptionInConstructor()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new StubEnvironment(runtime, messageBus);
			var factory = new DefaultSubsystemFactory();

			var exception = Assert.Throws<SubsystemLoadException>(() =>
				{
					factory.LoadSubsystem(
						typeof(SubsystemWithExceptionInConstructor),
						environment);
				});

			Assert.That(exception.Message, Is.EqualTo(SubsystemResources.SubsystemLoadFailure));
			Assert.That(exception.GetBaseException().Message, Is.EqualTo("CTR"));
		}

		[Test]
		public void Test_LoadSubsystem_TypeWithExceptionInLoad()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new StubEnvironment(runtime, messageBus);
			var factory = new DefaultSubsystemFactory();

			var exception = Assert.Throws<SubsystemLoadException>(() =>
				{
					factory.LoadSubsystem(
						typeof(SubsystemWithExceptionInLoad),
						environment);
				});

			Assert.That(exception.Message, Is.EqualTo(SubsystemResources.SubsystemLoadFailure));
			Assert.That(exception.GetBaseException().Message, Is.EqualTo("LOAD"));
		}

		[Test]
		public void Test_LoadSubsystem_NullEnvironment()
		{
			var factory = new DefaultSubsystemFactory();

			Assert.Throws<ArgumentNullException>(() =>
				{
					factory.LoadSubsystem(
						typeof(MockSubsystem),
						null);
				});
		}

		[Test]
		public void Test_LoadSubsystem_EnvironmentNotMatching()
		{
			var runtime = new DummyRuntime();
			var messageBus = new DummyMessageBus();
			var environment = new RuntimeEnvironment(runtime, messageBus);
			var factory = new DefaultSubsystemFactory();

			var exception = Assert.Throws<SubsystemLoadException>(() =>
				{
					factory.LoadSubsystem(
						typeof(MockSubsystem),
						environment);
				});

			Assert.That(exception.Message, Is.EqualTo(SubsystemResources.SubsystemTypeDoesNotProvideSubsystemConstructor));
		}

	}
}

#endif
